@echo off
setlocal EnableExtensions DisableDelayedExpansion
title Windows Driver Policy Removal

REM Request elevation when double-clicked.
fltmc >nul 2>&1
if errorlevel 1 (
    echo Requesting administrator privileges...
    set "WDP_SCRIPT=%~f0"
    powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Start-Process -FilePath $env:WDP_SCRIPT -Verb RunAs -ErrorAction Stop } catch { exit 1 }"
    if errorlevel 1 (
        echo Administrator privileges were not granted. Operation cancelled.
        pause
        exit /b 1
    )
    exit /b 0
)

set "PolicyFile={8F9CB695-5D48-48D6-A329-7202B44607E3}.cip"
set "WindowsPolicy=%windir%\System32\CodeIntegrity\CiPolicies\Active\%PolicyFile%"
set "EfiMounted=0"
set "DeleteFailed=0"
set "FoundPolicy=0"

echo [1/4] Checking Secure Boot state...
set "SecureBootState=Unknown"
for /f "usebackq delims=" %%I in (`powershell -NoProfile -Command "try { Confirm-SecureBootUEFI -ErrorAction Stop } catch { 'Unknown' }"`) do set "SecureBootState=%%I"
if /i not "%SecureBootState%"=="False" (
    echo.
    echo Secure Boot is enabled or its state could not be determined. State: %SecureBootState%
    echo Disable Secure Boot in UEFI/BIOS and then run this script again.
    echo Back up your BitLocker or Device Encryption recovery key before proceeding.
    pause
    exit /b 1
)

echo Secure Boot is disabled.
echo Make sure your BitLocker or Device Encryption recovery key is backed up.
echo This script does not change BitLocker settings.
echo.

echo [2/4] Selecting a free drive letter and mounting the EFI partition...
set "EfiLetter="
for %%L in (S R Q P O N M) do (
    if not defined EfiLetter (
        if not exist "%%L:\" (
            mountvol | findstr /I /C:"%%L:\\" >nul || set "EfiLetter=%%L"
        )
    )
)
if not defined EfiLetter (
    echo No free drive letter is available. Operation cancelled.
    pause
    exit /b 1
)

mountvol %EfiLetter%: /s >nul
if errorlevel 1 (
    echo Failed to mount the EFI partition. Operation cancelled.
    pause
    exit /b 1
)
set "EfiMounted=1"
set "EfiPolicy=%EfiLetter%:\EFI\Microsoft\Boot\CiPolicies\Active\%PolicyFile%"
echo EFI partition mounted as %EfiLetter%:

if exist "%EfiPolicy%" set "FoundPolicy=1"
if exist "%WindowsPolicy%" set "FoundPolicy=1"
if "%FoundPolicy%"=="0" (
    echo Target Windows Driver Policy was not found. No deletion is needed.
    goto UnmountAndFinish
)

echo [3/4] Removing the specified policy files...
if exist "%EfiPolicy%" (
    del /f "%EfiPolicy%"
    if exist "%EfiPolicy%" (
        echo Failed to remove the policy from the EFI partition.
        set "DeleteFailed=1"
    ) else (
        echo Removed the policy from the EFI partition.
    )
) else (
    echo Target policy was not found on the EFI partition. Skipping.
)

if exist "%WindowsPolicy%" (
    del /f "%WindowsPolicy%"
    if exist "%WindowsPolicy%" (
        echo Failed to remove the policy from the Windows directory.
        set "DeleteFailed=1"
    ) else (
        echo Removed the policy from the Windows directory.
    )
) else (
    echo Target policy was not found in the Windows directory. Skipping.
)

:UnmountAndFinish
echo [4/4] Unmounting the EFI partition...
if "%EfiMounted%"=="1" (
    mountvol %EfiLetter%: /d >nul
    if errorlevel 1 (
        echo Failed to unmount the EFI partition. Run this later: mountvol %EfiLetter%: /d
        set "DeleteFailed=1"
    ) else (
        echo EFI partition unmounted.
    )
)

echo.
if "%DeleteFailed%"=="1" (
    echo The operation did not complete successfully.
    echo Do not delete other .cip files or change system file permissions.
    pause
    exit /b 1
)

if "%FoundPolicy%"=="0" (
    echo Check complete. No policy was deleted.
) else (
    echo The specified policy has been removed.
    echo Restart the computer and then re-enable Secure Boot.
    echo If BitLocker was suspended, run: manage-bde -protectors -enable %SystemDrive%
)
pause
exit /b 0
